<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Login_Model extends CI_Model
{
    function __construct()
    {
        parent::__construct();
    }
	
    function get_admin($user_name,$password)
	{
        $this->db->where('login_name',$user_name);
        $this->db->where('password',$password);
        $query = $this->db->get('sys_admin');
		$row 	= $query->row(); 
        if($row)
		{
			$rs 	= $this->mapuser($row) ;
			$this->session->sess_destroy();
			$this->session->sess_create(); 
			//Set session data 
			$this->session->set_userdata($rs);
			
			//Set logged_in to true
			$this->session->set_userdata(array('logged_in' => true));               
			return true;
        }else{
			return false;
        }          
    }
	
    function check_login($user_name,$password)
	{
        $this->db->where('login_name',$user_name);
        $this->db->where('password',$password);
        $query = $this->db->get('sys_admin');
        if($query->num_rows() > 0)
		{
            return true;
        }
        else
		{
            return false;
        }
    }
	
	private function mapuser($row)
	{
		$rs = new stdClass();
		$rs->admin_id = $row->admin_id ;
		$rs->admin_name = $row->login_name ;
		$rs->admin_fullname = $row->fullname ;
		$rs->admin_email = $row->email ;
		$rs->admin_level = $row->level ;
		return $rs ;
	}
}